class ConTeamAI extends UTTeamAI;

var ConqueredCastles CGAME;
var ConGRI CGRI;
var class<ConSquadAI> ConSquadType;
var ConSquadAI KnightSquad;
var ConSquadAI ArcherSquad;
var ConSquadAI ScoutSquad;

var bool bConObjectivesFound;
var int ConFoundPhase;

var ConObjective ImpSiege;
var ConObjective ImpCap;
var ConObjective ImpBattle;

// Kyle: Begin Administration Functions

function PostBeginPlay()
{
	`log("Con: AI - PostBeginPlay called");
	
	CGAME = ConqueredCastles(WorldInfo.Game);
	CGRI = ConGRI(CGAME.GameReplicationInfo);
	
	Super.PostBeginPlay();
}

function Timer()
{

	Super.Timer();
}

function ConReset()
{
	`log("Con: AI - ConReset called");
	bConObjectivesFound = false;
	ConFoundPhase = 999;
	
	ConInitObjectives();
}

function ConInitObjectives()
{
	local ConObjective O;
	local int i;
	
	ImpSiege = none;
	ImpCap = none;
	ImpBattle = none;
	
	`log("Con: AI - ConInitObjectives called");
	for (i = 0; i < CGAME.ConActiveObjectives.length; i++)
	{
		O = CGAME.ConActiveObjectives[i];
		`log("Con: AI - ConInitObjectives - Objective Checked "$O);
		
		switch (O.ConObjectiveType)
		{
			case CO_Dest:
					`log("Con: AI - ConInitObjectives - Case CO_Dest");
					if (ImpSiege == none || ImpSiege.ConImportance < O.ConImportance)
					{
						ImpSiege = O;
						`log("ImpSiege set to "$O);
					}
				break;
			case CO_Hold:
					`log("Con: AI - ConInitObjectives - Case CO_Hold");
					if (ImpCap == none || ImpCap.ConImportance < O.ConImportance)
					{
						ImpCap = O;
						`log("ImpCap set to "$O);
					}
				break;
				
			case CO_King:
					`log("Con: AI - ConInitObjectives - Case CO_King");
						`log("ImpBattle set to"$O);
				break;
			default:
					`log("Con: AI - ConInitObjectives - SWITCH DEFAULTED");
				break;
		}
	}
	
	`log("Con: AI - ConInitObjectives - Called ConInitSquads");
	ConInitSquads();
}

function ConInitSquads()
{
	local ConPRI CPRI;
	local ConAIController AI;
	local ConSquadAI S;
	
	KnightSquad = none;
	ArcherSquad = none;
	ScoutSquad = none;
	
	foreach WorldInfo.AllActors(class'ConSquadAI', S)
	{
		`log("Con: AI - ConInitSquads - Removing Squad "$S);
		RemoveSquad(S);
	}
	
	`log("Con: AI - ConInitSquads called");
	foreach WorldInfo.AllControllers(class'ConAIController', AI)
	{
		CPRI = ConPRI(AI.PlayerReplicationInfo);
		`log("Con: AI - ConInitSquads - Controller Checked "$AI.GetHumanReadableName());
		
		switch (CPRI.ConClass)
		{
			case "Knight":
					`log("Con: AI - ConInitSquads - Case Knight");
					if (KnightSquad == none)
					{
						KnightSquad = ConAddSquad(AI, ImpSiege);
						`log("Con: AI - ConInitSquads - KnightSquad set "$KnightSquad);
					} else {
						`log("Con: AI - KnightAddBot called "$AI.GetHumanReadableName());
						KnightSquad.AddBot(AI);
					}
				break;
			case "Archer":
					`log("Con: AI - ConInitSquads - Case Archer");
					if (ArcherSquad == none)
					{
						ArcherSquad = ConAddSquad(AI, ImpSiege);
						`log("Con: AI - ConInitSquads - ArcherSquad set "$ArcherSquad);
					} else {
						`log("Con: AI - Archer called "$AI.GetHumanReadableName());
						ArcherSquad.AddBot(AI);
					}
				break;
			case "Scout":
					`log("Con: AI - ConInitSquads - Case Scout");
					if (ScoutSquad == none)
					{
						ScoutSquad = ConAddSquad(AI, ImpSiege);
						`log("Con: AI - ConInitSquads - ScoutSquad set "$ScoutSquad);
					} else {
						`log("Con: AI - Scout called "$AI.GetHumanReadableName());
						`log("Con: AI - ConInitSquads - ScoutSquad set "$ScoutSquad);
						ScoutSquad.AddBot(AI);
					}
				break;
			default:
				`log("Con: AI - ConInitSquads - SWITCH DEFAULTED");
		}
	}
	ConSetTasks();
}

function ConSetTasks()
{
	if (ImpSiege != none)
	{
		`log("Con: AI - ConSetTasks - Setting all to "$ImpSiege);
		KnightSquad.SetObjective(ImpSiege, true);
		ArcherSquad.SetObjective(ImpSiege, true);
		ScoutSquad.SetObjective(ImpSiege, true);
	} else {
		if (ImpCap != none)
		{
			`log("Con: AI - ConSetTasks - Setting all to "$ImpCap);
			KnightSquad.SetObjective(ImpCap, true);
			ArcherSquad.SetObjective(ImpCap, true);
			ScoutSquad.SetObjective(ImpCap, true);
		} else {
			// If ImpBattle is none
			`log("Con: AI - ConSetTasks - No Task Found!");
		}
	}
}

// Kyle: End Administration Functions

// Kyle: Begin Strategic Functions

function ReAssessStrategy()
{
	`log("Con: AI - ReAssessStrategy called");
	if (CGRI.ConSiegePhase != ConFoundPhase)
	{
		`log("Con: AI - ReAssessStrategy calling ConInitObjectives");
		ConInitObjectives();
		ConFoundPhase = CGRI.ConSiegePhase;	
	}
}

// Kyle: End Strategic Functions

// Kyle: Begin Squad Administration

function ConSquadAI ConAddSquad(Controller C, ConObjective O)
{
	local ConSquadAI S;
	
	`log("Con: AI - ConAddSquad Controller "$C.GetHumanReadableName()$" Objective "$O);
	S = spawn(ConSquadType);
	S.Initialize(Team,O,C);
	S.NextSquad = Squads;
	Squads = S;
	`log("Con: AI - PostBeginPlay returning "$S);
	return S;
}

// Kyle: End Squad Administration

// Kyle: Begin Order Administration

function OrderBot_Siege(ConAIController AI)
{

}

function OrderBot_Capture(ConAIController AI)
{

}

function OrderBot_Defend(ConAIController AI)
{

}

// Kyle: End Order Administration

// Kyle: Begin Stripped Functions

function UTSquadAI AddSquadWithLeader(Controller C, UTGameObjective O)
{
	`log("Con: AI - Tried to AddSquadWithLeader");
	return none;
}

function UTSquadAI AddHumanSquad()
{
	`log("Con: AI - Tried to AddHumanSquad");
	return none;
}

function PutBotOnSquadLedBy(Controller C, UTBot B)
{
	`log("Con: AI - Tried to PutBotOnSquadLedBy");
}

function bool PutOnDefense(UTBot B)
{
	`log("Con: AI - Tried to PutOnDefense");
	return false;
}

function PutOnOffense(UTBot B)
{
	`log("Con: AI - Tried to PutOnOffense");
}

function PutOnFreelance(UTBot B)
{
	`log("Con: AI - Tried to PutOnFreelance");
}

// Kyle: End Stripped Functions


DefaultProperties
{
	ConSquadType=ConSquadAI
	name="ConTeamAI"
}
